package pri.zjy.linkedList;

import pri.zjy.linkedList.model.ListNode;

/**
 * @author zhangjy
 * @description 反转链表
 * @date 2025/5/7 15:51
 */
public class ReverseList_206 {

    /**
     *
     */
    public ListNode reverseList3(ListNode head) {
        // TODO 递归

        return null;
    }

    /**
     * 个解：迭代-双指针
     * <p>
     * 思路：反转前更新next，后再后移其他指针
     */
    public ListNode reverseList2(ListNode head) {
        if (head == null) return null;

        ListNode pre = null, cur = head, next = null;
        while (cur != null) {
            next = cur.next;
            // 反转
            cur.next = pre;
            // 后移
            pre = cur;
            cur = next;
        }
        return pre;
    }

    /**
     * 个解：迭代-双指针
     * <p>
     * 思路：反转后再后移所有指针
     */
    public ListNode reverseList(ListNode head) {
        if (head == null) return null;

        ListNode pre = null, cur = head, next = cur.next;
        while (cur != null) {
            // 反转
            cur.next = pre;
            // 后移
            pre = cur;
            cur = next;
            if (cur != null) next = cur.next;
        }
        return pre;
    }

}
